package com.sigmazero13.bgjeeves;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.sigmazero13.bgjeeves.utils.FileArrayAdapter;

public class ImportDatabaseListChooser extends ListActivity {

	FileArrayAdapter adapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getFileList();
	}
	
	
	private void getFileList() {
		File fileDir = new File(this.getString(R.string.db_backup_loc));
		FileFilter filter = new FileFilter() {
			@Override
			public boolean accept(File pathname) {
				return pathname.getName().startsWith(ImportDatabaseListChooser.this.getString(R.string.db_file)) 
					&& !pathname.getName().endsWith("-shm")
					&& !pathname.getName().endsWith("-wal");
			}
		};
		
		List<File> files = Arrays.asList(fileDir.listFiles(filter));
		
		adapter = new FileArrayAdapter(this, R.layout.list_item, files);
		this.setListAdapter(adapter);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		File f = adapter.getFile(position);
		Intent i = new Intent();
		i.putExtra("filename", f.getName());
		setResult(RESULT_OK, i);
		finish();
	}
}
